Telegram Group & Telegram Channel
Инкремент с помощью __pos__

В Python нет операции инкремента ++ как в си-подобных языках, поэтому используется x += 1. Однако запись ++x является валидным кодом (но не x++), так как это просто два унарных оператора сложения.

При применении унарного плюса у объекта вызывается магический метод __pos__, то есть запись ++x можно понять как x.__pos__().__pos__(). Зная это, можно реализовать класс, который будет представлять число и поддерживать поведение инкремента.

Код на картинке может показаться сначала немного сложным, но лучше проследить логику и понять работу метода __pos__. Если реализовать все остальные необходимо магические методы, то может получится полноценный класс числа, но в продакшне такие приколы лучше не писать.

Подписывайтесь на канал 👉@pythonofff



tg-me.com/pythonofff/2938
Create:
Last Update:

Инкремент с помощью __pos__

В Python нет операции инкремента ++ как в си-подобных языках, поэтому используется x += 1. Однако запись ++x является валидным кодом (но не x++), так как это просто два унарных оператора сложения.

При применении унарного плюса у объекта вызывается магический метод __pos__, то есть запись ++x можно понять как x.__pos__().__pos__(). Зная это, можно реализовать класс, который будет представлять число и поддерживать поведение инкремента.

Код на картинке может показаться сначала немного сложным, но лучше проследить логику и понять работу метода __pos__. Если реализовать все остальные необходимо магические методы, то может получится полноценный класс числа, но в продакшне такие приколы лучше не писать.

Подписывайтесь на канал 👉@pythonofff

BY Python академия




Share with your friend now:
tg-me.com/pythonofff/2938

View MORE
Open in Telegram


Python академия Telegram | DID YOU KNOW?

Date: |

That growth environment will include rising inflation and interest rates. Those upward shifts naturally accompany healthy growth periods as the demand for resources, products and services rise. Importantly, the Federal Reserve has laid out the rationale for not interfering with that natural growth transition.It's not exactly a fad, but there is a widespread willingness to pay up for a growth story. Classic fundamental analysis takes a back seat. Even negative earnings are ignored. In fact, positive earnings seem to be a limiting measure, producing the question, "Is that all you've got?" The preference is a vision of untold riches when the exciting story plays out as expected.

Telegram hopes to raise $1bn with a convertible bond private placement

The super secure UAE-based Telegram messenger service, developed by Russian-born software icon Pavel Durov, is looking to raise $1bn through a bond placement to a limited number of investors from Russia, Europe, Asia and the Middle East, the Kommersant daily reported citing unnamed sources on February 18, 2021.The issue reportedly comprises exchange bonds that could be converted into equity in the messaging service that is currently 100% owned by Durov and his brother Nikolai.Kommersant reports that the price of the conversion would be at a 10% discount to a potential IPO should it happen within five years.The minimum bond placement is said to be set at $50mn, but could be lowered to $10mn. Five-year bonds could carry an annual coupon of 7-8%.

Python академия from id


Telegram Python академия
FROM USA